const { Controller } = require("egg");

const { getReqParams, getResBody, getResList, getPageParams, getReqToken } = require("../../utils");
const validRules = require("../../utils/valid/rules");
const { E_NoticeType } = require("../../utils/const");
const { getTagsStr } = require("../../scripts/note/core/creator/mysql/_utils");

const tableName = "note";
const tables = [
  {
    note: [
      "*",
      // `os_praise.status AS is_praise",
      // `os_belittle.status AS is_belittle",
      // `os_collect.status AS is_collect",
      // `os_share.status AS is_share",
      // `os_report.status AS is_report",
      `(SELECT status FROM operate_status WHERE operate_status.create_id = note.create_id AND type = ${E_NoticeType.praise}) AS is_praise`,
      `(SELECT status FROM operate_status WHERE operate_status.create_id = note.create_id AND type = ${E_NoticeType.belittle}) AS is_belittle`,
      `(SELECT status FROM operate_status WHERE operate_status.create_id = note.create_id AND type = ${E_NoticeType.collect}) AS is_collect`,
      `(SELECT status FROM operate_status WHERE operate_status.create_id = note.create_id AND type = ${E_NoticeType.share}) AS is_share`,
      `(SELECT status FROM operate_status WHERE operate_status.create_id = note.create_id AND type = ${E_NoticeType.report}) AS is_report`,
      // [`(SELECT COUNT(*) FROM operate_status WHERE type = ${E_NoticeType.comment})`, "is_comment"],
    ],
  },
  { "operate_status:os|LEFT JOIN operate_status os ON note.create_id=os.create_id": [] },
  // {"operate_status:os_praise": ["note.create_id=os_praise.create_id"]},
  // {"operate_status:os_belittle": ["note.create_id=os_belittle.create_id"]},
  // {"operate_status:os_collect": ["note.create_id=os_collect.create_id"]},
  // {"operate_status:os_share": ["note.create_id=os_share.create_id"]},
  // {"operate_status:os_report": ["note.create_id=os_report.create_id"]},
];
const filter = {
  // 其他条件，例：笔记未删除、用户未注销等
  // "sn.type|<=": 15,
};
module.exports = class UserController extends Controller {
  // 查详情
  async detail() {
    const { ctx, service } = this;
    const rules = { id: validRules.id };
    const params = getReqParams(ctx, rules);
    if (!params) return;
    let sql = `SELECT n.*,
os_praise.status AS is_praise,
os_belittle.status AS is_belittle,
os_collect.status AS is_collect,
os_share.status AS is_share,
os_report.status AS is_report
FROM note n
LEFT JOIN operate_status os_praise ON os_praise.create_id = n.id AND os_praise.type = 12
LEFT JOIN operate_status os_belittle ON os_belittle.create_id = n.id AND os_belittle.type = 13
LEFT JOIN operate_status os_collect ON os_collect.create_id = n.id AND os_collect.type = 14
LEFT JOIN operate_status os_share ON os_share.create_id = n.id AND os_share.type = 15
LEFT JOIN operate_status os_report ON os_report.create_id = n.id AND os_report.type = 18
WHERE n.id = '1';`;
    // const data = await service.common.detail(tables, { where: { ...params, ...filter } });
    const data = await service.common.detail(sql, { where: { ...params, ...filter } });
    ctx.body = getResBody({ data });
  }
  // 查列表
  async list() {
    const tagsStr = getTagsStr("n");
    const sql = `SELECT
    n.id, n.title, n.topic_ids, n.create_id,
    DATE_FORMAT(n.create_time, '%Y-%m-%d %H:%i:%s') AS create_time,
    n.update_id,
    DATE_FORMAT(n.update_time, '%Y-%m-%d %H:%i:%s') AS update_time,
    n.exposure_num, n.view_num, n.comment_num, n.praise_num,
    n.belittle_num, n.collect_num, n.share_num, n.report_num,
    n.read_seconds, n.score, n.audit_id, n.status,
    n.visible_type, n.history_id, n.tags, n.remark,
    ${tagsStr}
    FROM note n
    WHERE n.title LIKE '%1%'
    GROUP BY n.id
    ORDER BY n.score DESC, n.update_time DESC, n.create_time DESC
    LIMIT 0, 20;`;

    const { ctx, service } = this;
    const rules = {};
    const params = getReqParams(ctx, rules);
    if (!params) return;
    const { tableName, addParams } = ctx;
    const pageParams = getPageParams(params, tableName, addParams);
    const data = await service.common.list(sql, pageParams, ["tags"]);
    const { whereParams } = pageParams;
    const total = await service.common.countTotal(tableName, { where: whereParams });
    ctx.body = getResBody({ data: getResList(data, total, pageParams.limits) });
  }
};
